package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedTechParam;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaDeviceRelatedTechParamDao {

    /**
     * 根据 设备资源id查询
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Results(id = "pcbaDeviceRelatedDeviceTechResultMap", value = {
            @Result(column = "pcba_device_related_tech_param_id", property = "pcbaDeviceRelatedTechParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_resource_id", property = "pcbaDeviceResourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_key", property = "techDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_value", property = "techDeviceParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_value_audit", property = "techDeviceParamValueAudit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "bind_device_param_key", property = "bindDeviceParamKey", jdbcType = JdbcType.VARCHAR),
    })
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM + " where pcba_device_resource_id = #{pcbaDeviceResourceId} order by pcba_device_related_tech_param_id")
    List<PcbaDeviceRelatedTechParam> getAllByDeviceResourceId(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 新增
     *
     * @param pcbaDeviceRelatedTechParam
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM + "(pcba_device_resource_id,tech_device_param_key,pcba_device_type_id,tech_device_param_value,tech_device_param_value_audit,create_time,bind_device_param_key) values " +
            "(#{pcbaDeviceRelatedTechParam.pcbaDeviceResourceId},#{pcbaDeviceRelatedTechParam.techDeviceParamKey},#{pcbaDeviceRelatedTechParam.pcbaDeviceTypeId},#{pcbaDeviceRelatedTechParam.techDeviceParamValue},#{pcbaDeviceRelatedTechParam.techDeviceParamValueAudit},#{pcbaDeviceRelatedTechParam.createTime},#{pcbaDeviceRelatedTechParam.bindDeviceParamKey})")
    Integer addDeviceRelatedParam(@Param("pcbaDeviceRelatedTechParam") PcbaDeviceRelatedTechParam pcbaDeviceRelatedTechParam);

    /**
     * 更新
     *
     * @param pcbaDeviceRelatedTechParam
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM +
            "<set>" +
            "<if test='pcbaDeviceRelatedTechParam.pcbaDeviceResourceId != null and pcbaDeviceRelatedTechParam.pcbaDeviceResourceId != \"\" '>" +
            " pcba_device_resource_id = #{pcbaDeviceRelatedTechParam.pcbaDeviceResourceId}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParam.techDeviceParamKey != null and pcbaDeviceRelatedTechParam.techDeviceParamKey != \"\" '>" +
            " tech_device_param_key = #{pcbaDeviceRelatedTechParam.techDeviceParamKey}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParam.pcbaDeviceTypeId != null and pcbaDeviceRelatedTechParam.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaDeviceRelatedTechParam.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParam.techDeviceParamValue != null '>" +
            " tech_device_param_value = #{pcbaDeviceRelatedTechParam.techDeviceParamValue}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParam.techDeviceParamValueAudit != null '>" +
            " tech_device_param_value_audit = #{pcbaDeviceRelatedTechParam.techDeviceParamValueAudit}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParam.updateTime != null '>" +
            " update_time = #{pcbaDeviceRelatedTechParam.updateTime}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParam.bindDeviceParamKey != null and pcbaDeviceRelatedTechParam.bindDeviceParamKey != \"\" '>" +
            " bind_device_param_key = #{pcbaDeviceRelatedTechParam.bindDeviceParamKey}," +
            "</if>" +
            "</set>" +
            " where pcba_device_related_tech_param_id = #{pcbaDeviceRelatedTechParam.pcbaDeviceRelatedTechParamId}" +
            "</script>")
    Integer updateDeviceRelatedParam(@Param("pcbaDeviceRelatedTechParam") PcbaDeviceRelatedTechParam pcbaDeviceRelatedTechParam);

    /**
     * 根据pcbaDeviceResourceId 删除
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM + " where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    Integer deleteDeviceRelatedParam(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    @ResultMap(value = "pcbaDeviceRelatedDeviceTechResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM + " where pcba_device_resource_id = #{pcbaDeviceResourceId} and tech_device_param_key = #{techDeviceParamkey} limit 1")
    PcbaDeviceRelatedTechParam getPcbaDeviceRelatedTechParamByKeyAndDeviceResourceId(@Param("techDeviceParamkey") String techDeviceParamkey, @Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 清空对应资源的临时值（审核通过）
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM + " set tech_device_param_value_audit = '' where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    Integer clearValueAudit(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 将 value 还原成 ValueAudit 的值（审核不通过）
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM + " set tech_device_param_value = tech_device_param_value_audit where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    Integer returnValueFromValueAudit(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);
}
